package com.mb.app.service.impl;

import com.mb.app.bo.AppChannelPvUvBO;
import com.mb.app.dao.IAppChannelPvUvMapper;
import com.mb.app.dao.TbReportAppChannelMapper;
import com.mb.app.domain.TbReportAppChannel;
import com.mb.app.service.IAppChannelPvUvService;
import com.mb.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 应用渠道PVUV跑批服务实现
 * @author liliping
 * @create 2019-03-27 2:08 PM
 **/
@Service("appChannelPvUvService")
@Slf4j
public class AppChannelPvUvServiceImpl implements IAppChannelPvUvService {

    @Autowired
    private IAppChannelPvUvMapper appChannelPvUvMapper;

    @Autowired
    private TbReportAppChannelMapper reportAppChannelMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int runReport() {
        String reportDate = DateUtils.format(new Date(),"yyyy-MM-dd");
        Map<String,Object> map = new HashMap<String, Object>();

        List<AppChannelPvUvBO> list = appChannelPvUvMapper.getAppInfo();
        if(list != null && list.size() != 0) {
            Iterator<AppChannelPvUvBO> it = list.iterator();
            while (it.hasNext()){
                AppChannelPvUvBO appChannelPvUvBO = it.next();
                Long appId = appChannelPvUvBO.getAppId();
                Long appChannelId = appChannelPvUvBO.getAppChannelId();

                //delete
                Map<String,Object> delMap = new HashMap<>();
                delMap.put("report_date",reportDate);
                delMap.put("app_id",appId);
                delMap.put("app_channel_id",appChannelId);
                reportAppChannelMapper.deleteByMap(delMap);

                map.put("reportDate",reportDate);
                map.put("appId",appId);
                map.put("appChannelId",appChannelId);

                // 当日注册数量
                Integer todayRegCount = this.getTodayRegCount(map);

                //登录成功
                Integer loginCount = this.getLoginCount(map);

                //登录转化率
                BigDecimal loginConvertRate = this.getLoginConvertRate(loginCount,todayRegCount);


                //点击人数
                Integer userClickCount = this.getUserClickCount(map);

                //点击人数占比 点击人数/注册用户
                BigDecimal clickRate = this.getClickRate(loginCount,todayRegCount);

                //点击次数
                Integer clickTimes = this.getClickTimes(map);

                //老用户点击人数
                Integer oldUserClickCount = this.getOldUserClickCount(map);

                //老用户点击次数
                Integer oldUserClickTimes = this.getOldUserClickTimes(map);

                //人均点击次数 点击次数/反馈用户
                Integer avgClickTimes = this.getAvgClickTimes(clickTimes,todayRegCount);


                TbReportAppChannel reportAppChannel = new TbReportAppChannel();
                reportAppChannel.setReportDate(reportDate);
                reportAppChannel.setAppId(appId);
                reportAppChannel.setAppChannelId(appChannelId);

                reportAppChannel.setTodayRegCount(todayRegCount);
                reportAppChannel.setTodayLoginCount(loginCount);
                reportAppChannel.setLoginConvertRate(loginConvertRate);
                reportAppChannel.setUserClickCount(userClickCount);
                reportAppChannel.setUserClickRate(clickRate);
                reportAppChannel.setClickTimes(clickTimes);
                reportAppChannel.setOldUserClickCount(oldUserClickCount);
                reportAppChannel.setOldUserClickTimes(oldUserClickTimes);
                reportAppChannel.setAvgClickTimes(avgClickTimes);

                reportAppChannelMapper.insert(reportAppChannel);

            }
        }
        return 1;
    }

    private Integer getTodayRegCount(Map<String,Object> map){
        return appChannelPvUvMapper.getTodayRegCount(map);
    }

    private Integer getLoginCount(Map<String,Object> map){
        return appChannelPvUvMapper.getLoginCount(map);
    }


    private BigDecimal getLoginConvertRate(Integer loginCount,Integer todayRegCount){
        if(todayRegCount == null || todayRegCount.intValue() == 0){
            return new BigDecimal(0);
        }

        BigDecimal loginConvertRate = new BigDecimal(0);
        loginConvertRate = new BigDecimal(loginCount).divide(new BigDecimal(todayRegCount));
        loginConvertRate = loginConvertRate.setScale(2,BigDecimal.ROUND_HALF_DOWN);
        return loginConvertRate;
    }

    private Integer getUserClickCount(Map<String,Object> map){
        return appChannelPvUvMapper.getUserClickCount(map);
    }

    private BigDecimal getClickRate(Integer clickCount,Integer todayRegCount){
        if(todayRegCount == null || todayRegCount.intValue() == 0){
            return new BigDecimal(0);
        }

        BigDecimal clickRate = new BigDecimal(0);
        clickRate = new BigDecimal(clickCount).divide(new BigDecimal(todayRegCount));
        clickRate = clickRate.setScale(2,BigDecimal.ROUND_HALF_DOWN);
        return clickRate;
    }

    public Integer getClickTimes(Map<String,Object> map){
        return appChannelPvUvMapper.getClickTimes(map);
    }


    public Integer getOldUserClickCount(Map<String,Object> map){
        return appChannelPvUvMapper.getOldUserClickCount(map);
    }

    public Integer getOldUserClickTimes(Map<String,Object> map){
        return appChannelPvUvMapper.getOldUserClickTimes(map);
    }

    private Integer getAvgClickTimes(Integer clickTimes,Integer todayRegCount){
        if(todayRegCount == null || todayRegCount.intValue() == 0){
            return new Integer(0);
        }
        BigDecimal avgclickTimes = new BigDecimal(clickTimes).divide(new BigDecimal(todayRegCount));
        return new Integer(avgclickTimes.intValue());
    }
}
